package com.jinglvhuanjing.app.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.jinglvhuanjing.app.R;
import com.jinglvhuanjing.app.adapter.SpoutSwitchAdapter;
import com.jinglvhuanjing.app.bean.SpoutSwitchBean;
import com.jinglvhuanjing.app.bean.param.ParamLayout5Bean;
import com.jinglvhuanjing.app.listener.SwitchEvent;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class ParamSettingRow5View extends ConstraintLayout {

    private static final String TAG = "common";

    public ParamSettingRow5View(@NonNull Context context) {
        super(context);
        this.init(context);
    }

    public ParamSettingRow5View(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.init(context);
    }

    public ParamSettingRow5View(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.init(context);
    }

    /*** 投口选择 - 零点校准 - 按钮 ***/
    private AppCompatTextView device_control_tk_ldxz_label;
    /*** 投口选择 - 零点校准 - 下拉框 ***/
    private MySpinnerView device_control_ldxz;
    /*** 投口选择 - 砝码校准 - 按钮 ***/
    private AppCompatTextView device_control_fmxz;
    /*** 投口选择 - 砝码校准 - 输入框 ***/
    private CustomTextView device_control_fm_edit;
    /*** 投口控制列表  ***/
    private RecyclerView mSpoutSwitchListView;
    /*** 扫码控制  ***/
    private CustomTextView smkz;
    /*** 信号输出  ***/
    private SignalOutPanelView sign_out_panel;

    private SpoutSwitchAdapter mSpoutSwitchAdapter;
    private SwitchEvent mSpoutSwitchEvent;

    private void init(Context context) {
        LayoutInflater.from(context).inflate(R.layout.view_params_setting_row_5_layout, this, true);
        this.device_control_tk_ldxz_label = findViewById(R.id.device_control_tk_ldxz_label);
        this.device_control_ldxz = findViewById(R.id.device_control_ldxz);
        this.device_control_fmxz = findViewById(R.id.device_control_fmxz);
        this.device_control_fm_edit = findViewById(R.id.device_control_fm_edit);
        this.sign_out_panel = findViewById(R.id.sign_out_panel);
        this.smkz = findViewById(R.id.device_control_smkz);
        this.mSpoutSwitchListView = findViewById(R.id.device_spout_switch_list);
        this.mSpoutSwitchListView.setLayoutManager(new GridLayoutManager(context, 3));
        this.mSpoutSwitchAdapter = new SpoutSwitchAdapter();
        this.mSpoutSwitchListView.setAdapter(this.mSpoutSwitchAdapter);
        this.initEvent();
        this.testData();

        this.device_control_fm_edit.setParamTag("设备控制_称重校准_砝码校准", ParamLayout5Bean.PARAM_KEY_DEVICE_CONTROL_FM_EDIT);
        this.smkz.setParamTag("设备控制_扫码控制", ParamLayout5Bean.PARAM_KEY_DEVICE_CONTROL_SMKZ);
        this.device_control_ldxz.setParamTag("设备控制_称重校准_零点校准", ParamLayout5Bean.PARAM_KEY_DEVICE_CONTROL_LDXZ);
    }

    private void initEvent() {
        this.mSpoutSwitchAdapter.setOnItemClickListener((adapter, view, position) -> {
            SpoutSwitchBean spoutSwitchBean = this.mSpoutSwitchAdapter.getItem(position);
            boolean isChecked = spoutSwitchBean.isChecked();
            spoutSwitchBean.setChecked(!isChecked);
            this.mSpoutSwitchAdapter.notifyItemChanged(position);
            if (this.mSpoutSwitchEvent == null) {
                return;
            }
            int tkIndex = position + 1;
            Log.d(TAG, String.format("开关切换： LABEL = 设备控制_投口控制_%s号投口 , KEY = %s_%s , SWITCH_STATUS = %s", tkIndex, ParamLayout5Bean.PARAM_KEY_DEVICE_CONTROL_TK_CONTROL, tkIndex, spoutSwitchBean.isChecked()));
            this.mSpoutSwitchEvent.switchCallBack(String.format("设备控制_投口控制_%s号投口", tkIndex), String.format("device_control_tk_control_tkh_%s", tkIndex), spoutSwitchBean.isChecked());
        });
    }


    private void testData() {
        ArrayList<SpoutSwitchBean> items = new ArrayList<>();
        SpoutSwitchBean bean;
        for (int i = 0; i < 5; i++) {
            bean = new SpoutSwitchBean();
            bean.setName(String.format("%s号投口", i + 1));
            items.add(bean);
        }
        this.mSpoutSwitchAdapter.replaceData(items);
    }

    /**
     * 获取值
     *
     * @return 集合
     */
    public ArrayList<HashMap<String, String>> getFormValue() {
        ArrayList<HashMap<String, String>> values = new ArrayList<>();
        values.add(this.device_control_ldxz.getValueMap());
        values.add(this.device_control_fm_edit.getValueMap());
        values.add(this.smkz.getValueMap());

        List<SpoutSwitchBean> spoutSwitchBeans = mSpoutSwitchAdapter.getData();
        HashMap<String, String> spoutSwitchMap = new HashMap<>();
        int tkIndex = 1;
        for (SpoutSwitchBean spoutSwitchBean : spoutSwitchBeans) {
            spoutSwitchMap.put(String.format("device_control_tk_control_tkh_%s", tkIndex), spoutSwitchBean.isChecked() ? "1" : "0");
            tkIndex++;
        }
        values.add(spoutSwitchMap);
        values.addAll(this.sign_out_panel.getFormValue());
        return values;
    }

    public void addSpoutSwitchEvent(SwitchEvent switchEvent) {
        this.mSpoutSwitchEvent = switchEvent;
    }

    public void addSignalOutSwitchEvent(SwitchEvent switchEvent) {
        this.sign_out_panel.addSwitchEvent(switchEvent);
    }
}
